package br.com.imob.test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import br.com.imob.model.ImobItem;
import br.com.imob.util.Imob;

public class HTMLParser {
	
	@Test
	public void collect() throws Exception {
		
		Document doc = Jsoup.connect("http://multi.com.br/?pg=busca&escolha=alugar&faixaDePreco=fx0&ordenarPor=4").get();
		
		Element ulElement = doc.getElementById("listaImoveis");
		Elements liElements = ulElement.getElementsByTag("li");
		for( Element eImobs : liElements ) {
				
				ImobItem item = new ImobItem();
				
				Element code = eImobs.getElementById("codigo");
				item.setCode( code != null ? code.text() : null );
				
				Element neighbor = eImobs.getElementById("bairro");
				item.setNeighbor( neighbor != null ? neighbor.text() : null );
				
				Element price = eImobs.getElementById("valor");
				item.setPrice( price != null ? Float.parseFloat(price.text()) : null );
								
				Element photo = eImobs.getElementById("foto");
				item.setThumbnail( photo != null ? photo.child(0).absUrl("src") : null );
								
				Elements links = eImobs.select("a");
				if( links.size() > 0 ){
					item.setLink( links.first().attr("abs:href") );
				}
				
				item.setImob( Imob.MULTI.toString() );
				
				System.out.println( item );
		}
		System.out.println("==> " + liElements.size() );
		
	}
	

}
